<template>
  <div class="categories-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="showAddDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <zk-table
        class="table"
        border
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
        show-index
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="cateList"
        :columns="columns">
        <template slot="isOK" slot-scope="scope">
          <i v-if="!scope.row.cat_deleted" class="el-icon-success" style="color: lightgreen"></i>
          <i v-else class="el-icon-error" style="color: red"></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" size="mini" type="success">二级</el-tag>
          <el-tag v-else size="mini" type="warning">三级</el-tag>
        </template>
        <template slot="controller">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </zk-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      @close="resetAddRuleForm"
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%">
      <el-form :model="addRuleForm" :rules="addRules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            :collapse-tags="true"
            @change="cateHandleChange"
            v-model="selectCate"
            :options="parentCateList"
            :props="propsParent"
            clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      total: 0,
      paramsInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      currentPage: 1,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'controller'
        }
      ],
      addDialogVisible: false,
      addRuleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addRules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      propsParent: {
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'click'
      },
      selectCate: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.paramsInfo
      })
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.paramsInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.paramsInfo.pagenum = val
      this.getCateList()
    },
    async showAddDialog() {
      this.addDialogVisible = true

      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })

      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.parentCateList = res.data
    },
    cateHandleChange() {
      if(this.selectCate.length > 0) {
        this.addRuleForm.cat_pid = this.selectCate[this.selectCate.length - 1]
        this.addRuleForm.cat_level = this.selectCate.length
      } else {
        this.addRuleForm.cat_pid = 0
        this.addRuleForm.cat_level = 0
      }
    },
    addCate() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('categories', this.addRuleForm)
          if(res.meta.status !== 201) return this.$message.error(res.meta.msg)

          this.$message.success(res.meta.msg)
          this.getCateList()
          this.addDialogVisible = false
        } else {
          this.$message.error('提交失败')
          return false;
        }
      });
    },
    resetAddRuleForm() {
      this.$refs.ruleFormRef.resetFields()
      this.selectCate = []
      this.addRuleForm.cat_pid = 0
      this.addRuleForm.cat_level = 0
    }
  }
}
</script>

<style lang="less">
  .categories-container{
    .box-card{
      margin-top: 20px;
    }
    .table{
      margin: 20px 0;
    }
    .el-cascader{
      width: 100%;
    }
  }
  .el-cascader__dropdown .el-scrollbar,
  .el-cascader__dropdown .el-scrollbar__wrap {
    height: 300px;
  }
</style>